//
//  PMinePromotionViewController.m
//  Btpin
//
//  Created by Vic on 2019/9/25.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMinePromotionViewController.h"
#import "PMinePromotionCell.h"
#import "GeneralWebViewViewController.h"
#import "CompanyJobModel.h"
#import "PMinePromotionJobDetailViewController.h"

@interface PMinePromotionViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSMutableArray *itemDataArray;
    
    NSInteger currentPage;
}
@property (nonatomic, strong) UITableView *cTableView;
@end

@implementation PMinePromotionViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"职位推广";
    itemDataArray = [NSMutableArray arrayWithCapacity:0];
    currentPage = 1;
    
    [self initUI];
    [self loadData];
}
- (void)loadData {
    self.noDataView.hidden = YES;
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:[NSString stringWithFormat:@"%ld", (long)currentPage] forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:Person_PromoteList_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSArray class]]) {
                if (self->currentPage==1) {
                    if (self->itemDataArray.count) {
                        [self->itemDataArray removeAllObjects];
                    }
                }
                for (NSDictionary *dic in data) {
                    CompanyJobModel *model = [[CompanyJobModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [self->itemDataArray addObject:model];
                }
                self.noDataView.hidden = YES;
                [self.cTableView.mj_footer endRefreshing];
                [self.cTableView reloadData];
            }
            if (self->itemDataArray.count) {
            } else {
                self.noDataView.hidden = NO;
                [self.noDataView vcNilStatusType:VCNilStatusTypeOfCommonNoData superView:self.view];
            }
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
        [self.noDataView vcNilStatusType:VCNilStatusTypeOfNoNet superView:self.view];
    }];
}
- (void)initUI {
    UIButton *explainBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36*Scale) imageName:nil target:self action:@selector(explainBtnTap) title:@"赚钱说明" titleColor:THEMECOLOR_OF_TEXT_9a9a9a];
    explainBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:explainBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UIImageView *topImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(0, 0, WIDTH, ScalePS(43)) imageName:@"p_promotion_top_icon"];
    topImgView.userInteractionEnabled = NO;
    [self.view addSubview:topImgView];
    
    self.cTableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, EndY(topImgView), WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height-ViewHeight(topImgView)) target:self style:UITableViewStylePlain];
    self.cTableView.rowHeight = JobCellHeight;
    self.cTableView.tableFooterView = [[UIView alloc] init];
    self.cTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.cTableView];
    __weak __typeof(self)weakSelf = self;
    self.cTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self->currentPage++;
        [weakSelf.cTableView.mj_footer beginRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadData];
        });
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return itemDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PMinePromotionCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[PMinePromotionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
    }
    
    CompanyJobModel *model = itemDataArray[indexPath.row];
    [cell reloadDataForPromotionModel:model];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CompanyJobModel *model = itemDataArray[indexPath.row];
    
    PMinePromotionJobDetailViewController *PMPJDVC = [[PMinePromotionJobDetailViewController alloc] init];
    PMPJDVC.id = model.id;
    [self.navigationController pushViewController:PMPJDVC animated:YES];
}
- (void)explainBtnTap {
    GeneralWebViewViewController *GWVC = [[GeneralWebViewViewController alloc] init];
    GWVC.showType = GeneralWebViewTypeOfMakeMoneyExplain;
    GWVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:GWVC animated:YES];
}

@end
